package com.vlyman.demo.controller;

import com.vlyman.annotation.ValidateField;
import com.vlyman.annotation.ValidateGroup;
import com.vlyman.annotation.VlymanLog;
import com.vlyman.demo.model.entity.User;
import com.vlyman.demo.service.UserService;
import com.vlyman.rtn.RtnResult;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 类描述：
 *  用户信息控制类
 *  @author chenjs
 *  @date 2018/9/19 20:44
 **/
@Api(tags = "用户信息接口")
@RestController
@RequestMapping("/api/demo/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 方法描述：
     *   获取所有用户
     *  @author chenjs
     *  @return RtnResult
     *  @date 2018/9/19 20:45
     */
    @ApiOperation(value="获取所有用户", notes="不分页获取所有用户")
    @VlymanLog(description = "获取所有用户")
    @GetMapping("/list")
    public RtnResult getAllUsers(){
        return userService.getAllUser();
    }

    /**
     * 方法描述：
     *   保存用户
     *  @author chenjs
     *  @param user
     *  @return RtnResult
     *  @date 2018/10/9 23:18
     */
    @ApiOperation(value="保存用户", notes="保存用户插入数据库")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "用户id，无需填写"),
        @ApiImplicitParam(name = "name", value = "用户名", required = true),
        @ApiImplicitParam(name = "email", value = "用户邮箱地址", required = true),
        @ApiImplicitParam(name = "age", value = "年龄", required = true, dataTypeClass = Integer.class)
    })
    @VlymanLog(description = "保存用户")
    @ValidateGroup(fileds = {
        @ValidateField(fieldName = "name,email", notNull = true, notBlank = true),
        @ValidateField(fieldName = "age", notNull = true, minVal = 0)
    })
    @PostMapping("")
    public RtnResult saveUser(User user){
        return userService.saveUser(user);
    }

    /**
     * 方法描述：
     *   获取用户
     *  @author chenjs
     *  @param userId
     *  @return RtnResult
     *  @date 2018/10/10 11:57
     */
    @ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
    @ApiImplicitParam(name = "userId", required = true, dataTypeClass = Integer.class, paramType = "path")
    @VlymanLog(description = "获取用户")
    @ValidateGroup(fileds = {
        @ValidateField(minVal = 1)
    })
    @GetMapping("/{userId}")
    public RtnResult getUserById(@PathVariable("userId") int userId){
        return userService.getUserById(userId);
    }
}
